package com.mao.datastructure.arrays;

import android.os.Build;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;

public class ArrayListTest {

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void main(String[] args) {

        /**
         * 底层数组存储（线性表），在内存表现为连续的一块存储单元。默认初始化长度为10
         * 如果指定初始化长度会默认生成给定长度的数组，分配相应的内存单元
         *  private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
         *
         * Increases the capacity to ensure that it can hold at least the
         *  number of elements specified by the minimum capacity argument.
         *   private void grow(int minCapacity) {
         *         // overflow-conscious code
         *         int oldCapacity = elementData.length;
         *         int newCapacity = oldCapacity + (oldCapacity >> 1);
         *         if (newCapacity - minCapacity < 0)
         *             newCapacity = minCapacity;
         *         if (newCapacity - MAX_ARRAY_SIZE > 0)
         *             newCapacity = hugeCapacity(minCapacity);
         *         // minCapacity is usually close to size, so this is a win:
         *         elementData = Arrays.copyOf(elementData, newCapacity);
         *     }
         *
         *  根据这个方法看出 arraylist 扩容的后的大小是    Length + length>>1  ≈ 1 + 1/2
         *  右移算法是除以2的n次方
         *
         */
        ArrayList<String> list = new ArrayList<>(100);
        System.out.println("list size:"+list.size()); // 0  但是内部已经生声明了一个100长度的数组
        for (int i = 0; i < 30; i++) {
            list.add("value"+i);
        }
        System.out.println("list size:"+list.size());
        /**
         * 采用顺序遍历查找
         * public int indexOf(Object o) {
         *         if (o == null) {
         *             for (int i = 0; i < size; i++)
         *                 if (elementData[i]==null)
         *                     return i;
         *         } else {
         *             for (int i = 0; i < size; i++)
         *                 if (o.equals(elementData[i]))
         *                     return i;
         *         }
         *         return -1;
         *     }
         *
         */
        boolean hasAny  = list.contains("123");

        list.forEach(System.out::println);

        list.forEach(item->{
            if (item.contains("0")){
                System.out.println(item);
            }
        });



    }


}
